﻿<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
    <title>综合治税GISAPP-后台管理中心</title>
    <link rel="stylesheet" href="Content/src/css/layui.css" media="all">
    
</head>
<body>
<div class="layui-tab layui-tab-brief main-tab-container ">
    <div class="layui-tab-content">
        <button class="layui-btn layui-btn-normal layui-btn-small demo" data-type="新增"><i class="layui-icon">&#xe654;</i> 新 增 </button>
        <button class="layui-btn layui-btn-warm layui-btn-small demo" data-type="编辑"><i class="layui-icon">&#xe642;</i> 编 辑 </button>
        <button class="layui-btn layui-btn-danger layui-btn-small" lay-submit lay-filter="delete"><i class="layui-icon">&#xe640;</i> 删 除 </button>
    </div>
    <div class="layui-form layui-tab-content">
      <table class="layui-table">
        <colgroup>
          <col width="80">
        </colgroup>
        <thead>
          <tr>
            <th><input name="checkAll" lay-skin="primary" type="checkbox" lay-filter="checkAll" ></th>
            <th>序号</th>
            <th>用户名</th>
            <th>密码</th>
            <th>真实姓名</th>
            <th>所属街办</th>
            <th>发布时间</th>
            <!--<th>发布人</th>-->
          </tr> 
        </thead>
        <tbody id="showNewList">

        </tbody>
      </table>
    </div>
</div>

<!-- Mode -->
<div id="add_edit" style="display: none;padding: 20px;">
    <form class="layui-form">
        <div class=" layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" id="User_UserName" lay-verify="var_name" autocomplete="off"  class="layui-input">
                <input type="text" id="User_Id" hidden>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" id="User_Password" lay-verify="var_newpass" autocomplete="off" class="layui-input">
                <span>默认密码:123456</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">真是姓名</label>
            <div class="layui-input-block">
                <input type="text" id="User_RealName" lay-verify="var_realname" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属街办</label>
            <div class="layui-input-block">
                <select id="User_DtParentName" >
                    <option value="0" checked>请选择街办</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item" style="text-align: right;">
            <div class="layui-btn layui-btn-normal"  lay-submit lay-filter="btnAddRole">提 交</div>
        </div>
    </form>
</div>

<script type="text/javascript" src="Content/src/layui.js"></script>
<script src="Content/js/zor_base.js"></script>
<script type="text/javascript">
    layui.use(['element', 'layer', 'form', 'layedit'], function () {
        var element = layui.element()
        , jq = layui.jquery
        , form = layui.form()
        , layedit = layui.layedit
        , laypage = layui.laypage;

        var editIndex = layedit.build('User_Info');
       

        //加载，读取新闻列表
        jq(document).ready(function () {
            if (localStorage.getItem("username") == "" || localStorage.getItem("username") == null) {
                window.location.href = "login.html";
            } else {
                loading = layer.load();
                jq.ajax({
                    type: 'post',
                    url: '/api/Users/Userlist',
                    success: function (data) {
                        var html = '';
                        var accordionMenu = data;
                        jq.each(accordionMenu, function (i) {
                            html += "<tr>";
                            html += "<td><input name='' lay-skin='primary' type='checkbox' lay-filter='checkOne' role-id='" + accordionMenu[i].User_Id + "' role-name='" + accordionMenu[i].User_UserName + "' role-real='" + accordionMenu[i].User_RealName + "' role-dtparentname='" + accordionMenu[i].User_DtParentId + "'></td>";
                            html += "<td>" + (i + 1) + "</td>";
                            html += "<td>" + accordionMenu[i].User_UserName + "</td>";
                            html += "<td>******</td>";
                            html += "<td>" + accordionMenu[i].User_RealName + "</td>";
                            html += "<td>" + accordionMenu[i].User_DtParentName + "</td>";
                            html += "<td>" + getLocalTime(accordionMenu[i].User_CreateDate) + "</td>";
                            //html += "<td>" + accordionMenu[i].User_CreateName + "</td>";
                            html += "</tr>";
                        });

                        jq('#showNewList').empty().append(html);
                        form.render('checkbox');
                        layer.close(loading);
                    }
                });

                jq.ajax({
                    type: 'post',
                    url: '/api/PropertyInfo/StreetOffice',
                    success: function (data) {
                        var DtParentName = "<option value='0'>请选择街办</option>";
                        var accordionMenu = data;
                        jq.each(accordionMenu, function (i) {
                            DtParentName += "<option value='" + accordionMenu[i].Dt_ParentId + "'>" + accordionMenu[i].Dt_ParentName + "</option>";
                        });

                        jq('#User_DtParentName').empty().append(DtParentName);
                        form.render('select');
                    }
                });
                
            }
        });


        //监听提交- 删除
        form.on('submit(delete)', function (data) {
            //判断是否有选项
            var ary = check(jq);
            if (ary.length == 0) {
                layer.msg('请选择要删除的数据！');
                return false;
            }
            layer.confirm("确认删除吗", { icon: 2, btn: ['确定', '取消'] }, function (index, layero) {
                jq.ajax({
                    type: 'post',
                    url: '/api/Users/DeUers',
                    contentType: "application/json",
                    data: JSON.stringify({ 'us': ary }),
                    dataType: "json",
                    success: function (data) {
                        if (data.AnswerType == 1) {
                            layer.close(loading);
                        }
                        layer.msg(data.AnswerMeg, function () {
                            location.reload();
                            return false;
                        });
                        Replace();
                    }
                });
            });
            
        });


        //全选
        form.on('checkbox(checkAll)', function (data) {
            if (data.elem.checked) {
                jq("input[type='checkbox']").prop('checked', true);
            } else {
                jq("input[type='checkbox']").prop('checked', false);
            }
            form.render('checkbox');
        });


        //单选
        form.on('checkbox(checkOne)', function (data) {
            var is_check = true;
            if (data.elem.checked) {
                jq("input[lay-filter='checkOne']").each(function () {
                    if (!jq(this).prop('checked')) { is_check = false; }
                });
                if (is_check) {
                    jq("input[lay-filter='checkAll']").prop('checked', true);
                }
            } else {
                jq("input[lay-filter='checkAll']").prop('checked', false);
            }
            form.render('checkbox');
        });


        //显示弹出框 - 增加、修改
        jq('.demo').on('click', function () {

            var type = jq(this).data('type');

            if (type == '编辑') {
                var ary = check(jq);
                if (ary.length > 1) {
                    layer.msg('一次只能编辑一条信息，请重新选择！');
                    return;
                } else if (ary.length == 0) {
                    layer.msg('请选择数据行！');
                    return;
                }
            }

            layer.open({
                title: type,
                type: 1,
                skin: 'layui-layer-rim',
                area: ['550px', '400px'],
                content: jq('#add_edit'),
                success: function () {
                    if (type == '编辑') {
                        jq('#User_UserName').val(ary[0].User_UserName);
                        jq('#User_RealName').val(ary[0].User_RealName);
                        jq('#User_Password').val(ary[0].User_Password);
                        jq('#User_DtParentName').val(ary[0].User_DtParentId);
                        jq('#User_Id').val(ary[0].User_Id);
                    }
                    else {
                        jq('#User_UserName').val('');
                        jq('#User_RealName').val('');
                        jq('#User_Password').val('123456');
                        jq('#User_Id').val('');
                    }
                    form.render('select');
                }
            });

        });
        form.verify({
            var_name: [/^[a-zA-Z]{5,10}$/, '用户名为5~10位，必须以英文字母开头！'],
            var_realname: [/([\u4e00-\u9fa5]{2,4})/, '真实姓名必须为2~4位汉字！']

        });

        //提交新增、编辑
        form.on('submit(btnAddRole)', function (data) {

            var jsonData = layedit.getContent(editIndex);
            if (jq('#User_DtParentName').val() == "0") {
                layer.msg("请选择街办");
            }
            else {
                var Users = {
                    User_Id: jq('#User_Id').val(),
                    User_UserName: jq('#User_UserName').val(),
                    User_RealName: jq('#User_RealName').val(),
                    User_Password: jq('#User_Password').val(),
                    User_DtParentId: jq('#User_DtParentName').val(),
                    User_DtParentName: jq('#User_DtParentName').find("option:selected").text(),
                    User_CreateName: localStorage.getItem("userid")
                };
                loading = layer.load();
                jq.ajax({
                    type: 'POST',
                    url: '/api/Users/InserUs',
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify(Users),
                    success: function (data) {
                        layer.close(loading);
                        if (data.AnswerType == 1) {

                            layer.msg(data.AnswerMeg, function () {
                                Replace();
                            });
                        }
                        if (data.AnswerType == 2) {
                            layer.msg(data.AnswerMeg);
                        }
                    }
                });
            }
        });

        //返回 选择行的值
        function check() {
            var ary = new Array();
            jq("input[lay-filter='checkOne']").each(function () {
                if (jq(this).prop('checked')) {
                    var ckindex = jq(this).attr('role-id');
                    var cktname = jq(this).attr('role-name');
                    var ckreal = jq(this).attr('role-real');
                    var ckdtparentname = jq(this).attr('role-dtparentname');
                    ary.push({ User_Id: ckindex, User_UserName: cktname, User_RealName: ckreal, User_DtParentId: ckdtparentname });
                }
            });
            return ary;
        }
    })
</script>

</body>
</html>